package org.liangsy.client.user.controller;


import jakarta.servlet.http.HttpServletRequest;
import org.liangsy.client.user.common.ClientLoginConstans;
import org.liangsy.client.user.dto.UserInfoDto;
import org.liangsy.client.user.service.UserOperationLogService;
import org.liangsy.client.user.service.UserService;
import org.liangsy.common.bean.Result;
import org.liangsy.common.bean.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserOperationLogService userOperationLogService;

    @PostMapping("/pc/register")
    public ResponseEntity<Result> registerPc(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.register(userInfo);
        } catch (Exception e) {
            result = Result.failed(ResultCode.FAILED.getResultCode(), "注册失败：" + e.getMessage());
        } finally {
            userOperationLogService.weblogOperation(ClientLoginConstans.OPERATION_TYPE.REGISTRATION, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/pc/login")
    public ResponseEntity<Result> loginPc(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.login(userInfo);
        } catch (Exception e) {
            userOperationLogService.weblogOperation(ClientLoginConstans.OPERATION_TYPE.LOGIN, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/forgetPassword")
    public ResponseEntity<Result> forgetPasswordPc(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.forgetPassword(userInfo);
        } catch (Exception e) {
            userOperationLogService.weblogOperation(ClientLoginConstans.OPERATION_TYPE.RESET_PASSWORD, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/wx/register")
    public ResponseEntity<Result> registerWx(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.register(userInfo);
        } catch (Exception e) {
            result = Result.failed(ResultCode.FAILED.getResultCode(), "注册失败：" + e.getMessage());
        } finally {
            userOperationLogService.wxLogOperation(ClientLoginConstans.OPERATION_TYPE.REGISTRATION, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/wx/login")
    public ResponseEntity<Result> loginWx(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.login(userInfo);
        } catch (Exception e) {
            userOperationLogService.wxLogOperation(ClientLoginConstans.OPERATION_TYPE.LOGIN, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/wx/forgetPassword")
    public ResponseEntity<Result> forgetPasswordWx(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.forgetPassword(userInfo);
        } catch (Exception e) {
            userOperationLogService.wxLogOperation(ClientLoginConstans.OPERATION_TYPE.RESET_PASSWORD, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/app/register")
    public ResponseEntity<Result> registerApp(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.register(userInfo);
        } catch (Exception e) {
            result = Result.failed(ResultCode.FAILED.getResultCode(), "注册失败：" + e.getMessage());
        } finally {
            userOperationLogService.appLogOperation(ClientLoginConstans.OPERATION_TYPE.REGISTRATION, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/app/login")
    public ResponseEntity<Result> loginApp(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.login(userInfo);
        } catch (Exception e) {
            userOperationLogService.appLogOperation(ClientLoginConstans.OPERATION_TYPE.LOGIN, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping("/app/forgetPassword")
    public ResponseEntity<Result> forgetPasswordApp(@RequestBody UserInfoDto userInfo, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        Result result = new Result();
        try {
            result = userService.forgetPassword(userInfo);
        } catch (Exception e) {
            userOperationLogService.appLogOperation(ClientLoginConstans.OPERATION_TYPE.RESET_PASSWORD, userInfo.getUserId(), result, ipAddress);
        }
        return ResponseEntity.ok(result);
    }
}